//
//  MOCommonUseViewController.m
//  MobileOffice
//
//  Created by 晓 on 2019/5/30.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOCommonUseViewController.h"
#import "MOCommonUseAddViewController.h"
#import "MOCommonUseViewCell.h"
#import "MONoticeViewController.h"
#import "MOOfficeTextSearchViewController.h"

@interface MOCommonUseViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MOCommonUseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNav];
    
    [self creatView];
    
    [self initData];
    
    [self requestData];
}

-(void)setNav
{
    self.title = @"常用";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 38, 38)];
    [button setImage:IMAGE(@"use_add") forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickAddButton) forControlEvents:UIControlEventTouchUpInside];
//    [button setBackgroundColor:[UIColor redColor]];
    [button setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightItem;
}

-(void)creatView
{
    [self.view addSubview:self.collectionView];
}

-(void)clickAddButton
{
    MOCommonUseAddViewController *vc = [[MOCommonUseAddViewController alloc]init];
    vc.useDataArray = self.dataArray;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)initData
{
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestData) name:MENUCHANGESUCESS object:nil];
}

-(void)requestData
{
    
    NSArray *nameArr = @[@"104",@"105",@"106",@"107"];;
    NSArray *imageArr = @[@"use_daiban",@"use_daibanO",@"use_chaxun",@"use_jiandingwenan"];
    
    NSString *postUrl = [NSString stringWithFormat:@"%@/%@?authorization=%@",FINDFAVMENUBYUSERID_URL,[MOUser sharedMOUser].userName,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        
        NSLog(@"获取用户常用菜单:%@",responseObject);
        if ([responseObject isKindOfClass:[NSArray class]]) {
            self.dataArray = [NSMutableArray array];
            NSArray *arr = responseObject;
            for (int i = 0; i<arr.count; i++) {
                MOCommonUseModel *model = [[MOCommonUseModel alloc]init];
                model.nameStr = arr[i][@"menu_name"];
                model.imageStr = imageArr[[nameArr indexOfObject:arr[i][@"menu_id"]]];
                model.menuId = arr[i][@"menu_id"];
                model.itemIndex = i;
                [self.dataArray addObject:model];
            }
            [self.collectionView reloadData];
            if (arr.count==0) {
                [[ADShowMessage shareShowMessage] showTextOnly:@"暂无常用菜单" showInView:self.view];
            }
        }

    } failure:^(NSError *error) {
        NSLog(@"获取用户常用菜单:%@",[error localizedDescription]);
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
    
}

#pragma mark -UICollectionViewDataSource,UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MOCommonUseViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.item];
    return cell;
    
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(SCREEN_WIDTH/3, SCREEN_WIDTH/3);
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
}

//定义每个UICollectionView 横向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

//定义每个UICollectionView 竖向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(0,0);
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MOCommonUseModel *model = self.dataArray[indexPath.item];
    if ([model.menuId integerValue]==104) {//待办公文
        MONoticeViewController *vc = [[MONoticeViewController alloc]init];
        vc.typeStr = @"1";
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if ([model.menuId integerValue]==105){//已办公文
        MONoticeViewController *vc = [[MONoticeViewController alloc]init];
        vc.typeStr = @"2";
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.menuId integerValue]==106){//公文查询
        MOOfficeTextSearchViewController *vc = [[MOOfficeTextSearchViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if ([model.menuId integerValue]==107){//局内发布
        MONoticeViewController *vc = [[MONoticeViewController alloc]init];
        vc.typeStr = @"4";
        [self.navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark -懒加载
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-TabbarHeight-StatusBarAndNavigationBarHeight) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[MOCommonUseViewCell class] forCellWithReuseIdentifier:@"cell"];
        
    }
    return _collectionView;
}

@end
